package com.ciaojian.store.controller;

import cn.hutool.core.bean.BeanUtil;
import com.ciaojian.core.model.ao.DataDashboardAO;
import com.ciaojian.core.util.Result;
import com.ciaojian.store.service.MemberOrderServiceImpl;
import com.ciaojian.store.service.OrderServiceImpl;
import com.ciaojian.store.model.vo.DataDashboardVO;
import com.ciaojian.store.model.vo.OperationDashboardVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;

/**
 * @author Atlakyn
 */
@Api(tags = "首页")
@RestController
@RequestMapping("/home")
public class HomeController {
    @Resource
    private OrderServiceImpl orderService;
    @Resource
    private MemberOrderServiceImpl memberOrderService;

    @ApiOperation("首页数据")
    @GetMapping("/dashboard")
    public Result dashboard(@ApiParam("开始时间") @RequestParam("startDate") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startDate,
                            @ApiParam("结束时间") @RequestParam("endDate") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endDate,
                            @ApiParam("订单数组") @RequestParam("orderIds") Integer[] orderIds) {
        Assert.notNull(startDate, "请选择开始时间");
        Assert.notNull(endDate, "请选择结束时间");
        Assert.notNull(orderIds, "请选择店铺");
        Map<String, Object> map = orderService.homeDate(startDate, endDate, orderIds);
        return Result.ok(map);
    }

    @ApiOperation(value = "运营首页数据", response = DataDashboardVO.class)
    @PostMapping("/operation-dashboard")
    public Result operationDashboard(@Validated @RequestBody DataDashboardVO dataDashboardVO) {
        DataDashboardAO dataDashboardAo = new DataDashboardAO();
        BeanUtil.copyProperties(dataDashboardVO, dataDashboardAo);
        OperationDashboardVO operationDashboardVO = new OperationDashboardVO();
        switch (dataDashboardVO.getOrderType()) {
            case NORMAL:
            case BUSINESS:
            case MEMBER_DETAIL:
                // 统计次卡/免单支付的订单数
                operationDashboardVO.setFreeOrderNum(orderService.countFreeNum(dataDashboardAo));
                operationDashboardVO.setSumPayTotal(orderService.getSumPayTotal(dataDashboardAo));
                operationDashboardVO.setOrderNum(orderService.countOrderNum(dataDashboardAo));
                // 查生客
                dataDashboardAo.setCustomerType(3);
                operationDashboardVO.setFirst(orderService.countOrderNum(dataDashboardAo));
                // 查生客
                dataDashboardAo.setCustomerType(0);
                operationDashboardVO.setStranger(orderService.countOrderNum(dataDashboardAo));
                // 查熟客
                dataDashboardAo.setCustomerType(1);
                operationDashboardVO.setFamiliar(orderService.countOrderNum(dataDashboardAo));
                // 查深度熟客
                dataDashboardAo.setCustomerType(2);
                operationDashboardVO.setDeepFamiliar(orderService.countOrderNum(dataDashboardAo));
                break;
            case MEMBER:
                operationDashboardVO.setOrderNum(memberOrderService.countMemberOrder(dataDashboardAo));
                operationDashboardVO.setMemberMoneyTotal(memberOrderService.sumMoney(dataDashboardAo));
            default:
        }
        return Result.ok(operationDashboardVO);
    }
}
